﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using System.ServiceProcess;
using System.Text;
using Ramp.Service.Contract;
using Ramp.Service;

namespace LoadTestRampService
{
    /// <summary>
    /// This is a wrapper Windows Service that hosts the WCF RampService.
    /// </summary>
    public partial class LoadTestRampService : ServiceBase
    {
        /// <summary>
        /// Local member variable to track the running serviceHost.
        /// </summary>
        private ServiceHost serviceHost = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="LoadTestRampService"/> class.
        /// </summary>
        public LoadTestRampService()
        {
            InitializeComponent();
            if (!System.Diagnostics.EventLog.SourceExists("LoadTestRampService"))
            {
                System.Diagnostics.EventLog.CreateEventSource(
                    "LoadTestRampService", "Application");
            }
            eventLog.Source = "LoadTestRampService";
            eventLog.Log = "Application";
        }

        /// <summary>
        /// Executes when a Start command is sent to the service by the Service Control Manager (SCM) 
        /// or when the operating system starts (for a service that starts automatically). 
        /// Specifies actions to take when the service starts.
        /// </summary>
        /// <param name="args">Data passed by the start command.</param>
        protected override void OnStart(string[] args)
        {
            eventLog.WriteEntry("Attempting to start LoadTestRampService");
            if (serviceHost != null)
            {
                serviceHost.Close();
            }

            serviceHost = new ServiceHost(typeof(RampService));

            serviceHost.Open();            
        }

        /// <summary>
        /// Executes when a Stop command is sent to the service by the Service Control Manager (SCM). 
        /// Specifies actions to take when a service stops running.
        /// </summary>
        protected override void OnStop()
        {
            eventLog.WriteEntry("Attempting to stop LoadTestRampService");
            if (serviceHost != null)
            {
                serviceHost.Close();
                serviceHost = null;
            }            
        }
    }
}
